import json

from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from user.models import UserProfile


# Create your views here.
class UserViews(View):
    def get(self):
        pass
    def post(self,request):
        data = json.loads(request.body)
        print(data)
        username = data.get("username")
        email = data.get("email")
        phone = data.get("phone")
        sms_num = data.get("sms_num")
        password = data.get("password_1")
        password2 = data.get("password_2")
        if len(username) < 6 or len(username) > 11:
            return JsonResponse({"code": 10101, "error": "用户名不合法"})
        if len(password) < 6 or len(password) > 12:
            return JsonResponse({"code": 10102, "error": "密码不合法"})
        if len(phone) != 11:
            return JsonResponse({"code": 10103, "error": "手机号码不合法"})
        if password != password2:
            return JsonResponse({"code": 10104, "error": "两次密码不一样"})
        userquery = UserProfile.objects.filter(username=username)
        if userquery:
            return JsonResponse({"code": 10105, "error": "用户名被占用"})
        user = UserProfile.objects.create_user(username=username, email=email, phone=phone, password=password)
        result = {
            "code": 200,
            "username": username,
            "data": {}
        }
        return JsonResponse(result)









